package com.ljf.config;

import com.ljf.filter.LjfFilter;
import com.ljf.intercepter.EmpLoginInterceptor;
import com.ljf.util.PathUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class LjfWebMvcConfig implements WebMvcConfigurer {

    @Bean
    public EmpLoginInterceptor empLoginInterceptor(){
        return new EmpLoginInterceptor();
    }


   /* @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new EmpLoginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/checkedToken");
        WebMvcConfigurer.super.addInterceptors(registry);
    }*/

   /* @Bean
    public HasPermissionInterceptor hasPermissionInterceptor(){
        return new HasPermissionInterceptor();
    }*/
    @Autowired
    private LjfFilter ljfFilter;
    public FilterRegistrationBean filter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(ljfFilter);
        registrationBean.addUrlPatterns("/");
        return registrationBean;
    }
    @Bean
    public WebMvcConfigurer crossConfigurer(){
        return new WebMvcConfigurer() {
            @Override
            //重写父类提供的跨域请求处理的接口
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        //放行哪些原始域
                        .allowedOriginPatterns("*")
                        //是否发送cookie信息
                        .allowCredentials(true)

                        .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                        //放行哪些原始域（头部信息）
                        .allowedHeaders("*")
                        //暴露哪些头部信息
                        .exposedHeaders("Header1","Header2");
                WebMvcConfigurer.super.addCorsMappings(registry);
            }
        };
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String winPath = PathUtils.getClassLoadRootPath() + "/src/main/resources/static/files/";

        //第一个方法设置访问路径前缀，第二个方法设置资源路径
        registry.addResourceHandler("/files/**").
                addResourceLocations("file:" + winPath);
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}
